//
//  SAIndexFlowLayout.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/28.
//

#import "SAIndexFlowLayout.h"

@interface SAIndexFlowLayout()

@property (nonatomic, strong) NSArray<UICollectionViewLayoutAttributes *> *layoutAttrs;

@end


@implementation SAIndexFlowLayout

- (void)prepareLayout
{
    CGFloat itemW = 81;
    CGFloat itemH = 72;
    CGFloat itemSpacing = (kBaseScreenWidth - 243)/4;
    CGFloat lineSpacing = (257 - 216)/4;
    
    NSMutableArray *arrayM = [NSMutableArray array];
    for(int i = 0; i < self.dataList.count; i++)
    {
        int page, row, col;
        page = i / 9;
        row = (i % 9) / 3;
        col = (i % 9) % 3;
        
        CGRect cellFrame = SARectMake((itemSpacing + (itemSpacing+itemW) * col) + page * kBaseScreenWidth, itemH * row + lineSpacing * (row + 1), itemW, itemH);
        // 创建一个 UICollectionViewLayoutAttributes 对象
        NSIndexPath *idxPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:idxPath];
        attr.frame = cellFrame;
        
        [arrayM addObject:attr];
    }
    
    self.layoutAttrs = arrayM.copy;
    
    self.itemSize = SASizeMake(itemW, itemH);
}

- (CGSize)collectionViewContentSize
{
    NSInteger count = self.dataList.count;
    NSInteger pageCount = count%9 == 0 ? (count/9) : (count/9 + 1);
    return SASizeMake(kBaseScreenWidth * pageCount, 0);
}

- (NSArray <UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.layoutAttrs;
}



@end
